<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends CommonController 
{
    //role 角色管理 显示
    public function index()
    {
    	//个数
    	$count = M('auth_cate')->count();
    	$this->assign('num',$count);

    	//所有显示
    	$sql = 'select * from auth_cate order by cname';
    	$cate = M()->query($sql);
    	$this->assign('cate',$cate);

        $this->display();
    }

    //cate_add
    public function cate_add()
    {
    	//print_r($_POST);exit;
    	if (IS_POST) {
    		$data['cname'] = I('post.name');

        if (empty($data['cname'])) {
          $arr = array(
              "error" => 0,
              "info" => "权限类不能为空"
              );
          echo json_encode($arr);
          exit;
        }

    		$res = M('auth_cate')->add($data);
    		if ($res) {
	    		$arr = array(
				"error" => 1,
				"info" => "栏目创建成功"
	        	);
	       	echo json_encode($arr);
    		}else {
				$arr = array(
					"error" => 0,
					"info" => "栏目创建失败"
		        	);
		        echo json_encode($arr);
    		}

    	}
    }

    //编辑显示
    public function cateedit()
    {
    	$id = I('get.id');
    	$sql = 'select * from auth_cate where id='.$id;
    	$cateinfo = M()->query($sql);
    	$this->assign('cateinfo',current($cateinfo));

    	//栏目选择显示
        $sql = 'select * from auth_cate';
        $arr = M()->query($sql);
        $cate = $this->gettree($arr);
        $this->assign('cate',$cate);

    	$this->display();
    }
    //编辑
    public function edit()
    {
        //print_r($_POST);
        if (IS_POST) {
            $data['cname'] = I('post.cname');
            $data['fid'] = I('post.fid');

            $res = M('auth_cate')->where(array('id'=>I('post.cateid')))->save($data);
            if ($res) {
                $arr = array(
                "error" => 1,
                "info" => "栏目修改成功"
                );
            echo json_encode($arr);
            }else {
                $arr = array(
                    "error" => 0,
                    "info" => "栏目修改失败"
                    );
                echo json_encode($arr);
            }

        }
    }

    //彻底删除
    public function delete()
    {
       // print_r($_POST);exit;
      if (IS_POST) {
          $cateid = I('post.cateid');

          //分类表中id 在规则表有匹配到 不能删
          $sql = 'select * from auth_rule where cid='.$cateid;
          $cid = M()->query($sql);
          if ($cid) {
              $arr = array(
                  'error' =>1,
                  'info'=>"权限类中还存在规则,请先删除规则"
                  );
              echo json_encode($arr);
              exit;
          }

          $sql = 'delete from auth_cate where id='.$cateid;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"栏目彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"栏目彻底删除失败"
                  );
              echo json_encode($arr);
          }
      }
        
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        //分类表中id 在规则表有匹配到 不能删
        $sql = 'select * from auth_rule where cid in '.'('.$ids.')';
        $cid = M()->query($sql);
        if ($cid) {
            $arr = array(
                'error' =>1,
                'info'=>"权限类中还存在规则,请先删除规则"
                );
            echo json_encode($arr);
            exit;
        }
        //删除
        $res = M('auth_cate')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>0,
                  'info'=>"规则分类删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"规则分类删除失败"
                  );
              echo json_encode($arr);
          }

      }

    }    


}